if not menu then menu = {} end

if not menu.risk then


dofile (getScriptPath() .. "\\..\\lib\\lib.lua")


menu.risk = {lots = {		-- return volume in lots
		function(n) return 2 end,
		function(n) return n / 6 end,
		function(n) return n / 4 end,
		function(n) return n / 3 end,
		function(n) return n / 2 end,
		function(n) return n * 3 / 4 end,
		function(n) return n end
	},
	risk_limit = 1,
	changed = true
}


processor:AddInit(function()
	menu.risk.volume = {}
	
	for i, v in ipairs(menu.risk.lots) do
		table.sinsert(menu.risk.volume, 0)
	end
end)


processor:AddInit(function()
	window:AddRow()
	menu.risk.rows = #menu.risk.lots
	menu.risk.limits = {}
	menu.risk.row = window:AddRow()
	menu.risk.limits[1] = 0
	
	for i = 2, menu.risk.rows do
		window:AddRow()
		menu.risk.limits[i] = menu.risk.lots[i](menu.risk.risk_limit)
	end
end)


processor:AddInit(function()
	menu.risk:Select(1)
end)


processor:AddHandler("OnTableNotification", function(data)
	if data.msg == 11 and data.par1 >= menu.risk.row
		and data.par1 < menu.risk.row + menu.risk.rows
	then
		menu.risk:Select(data.par1 - menu.risk.row + 1)
	end
end)


processor:AddTimer(function()
	if menu.risk.changed
		or menu.chart.instrument
		and (menu.chart.changed.selected
			or menu.label.changed.lm
			or menu.label.changed.sl
			or menu.portfolio.changed
		)
	then
		window:SetCell(menu.label.show.lm.row, 3, "")
		
		local lots
		local r, max_lots, commission = lib:CalcRisk(menu.chart.instrument, menu.label.price.lm, menu.label.price.sl)
		
		if r and max_lots and commission then
			menu.portfolio.commission = commission
			window:SetCell(menu.label.show.lm.row, 3, string.format("-%.3f%%", commission * 100))
			
			if r > menu.risk.risk_limit then
				lots = int(max_lots * menu.risk.risk_limit / r)
			else
				lots = max_lots
			end
		end
			
		menu.risk.risks = {}
		
		for i, v in ipairs(menu.risk.lots) do
			if lots and lots >= 2
				and menu.portfolio.assets
				and menu.label.price.lm
				and menu.label.price.sl
			then
				menu.risk.volume[i] = menu.risk.lots[i](lots)
				menu.risk.volume[i] = int(menu.risk.volume[i] - menu.risk.volume[i] % 2)
				menu.risk.risks[i] = math.abs(menu.label.price.lm - menu.label.price.sl)
					* (1 + commission)
					* menu.risk.volume[i]
					* lib:GetLotSize(menu.chart.instrument)
					* 100 / menu.portfolio.assets
					
				window:SetCell(menu.risk.row + i - 1, 1, string.format("%.3f%%", menu.risk.risks[i]))
				window:SetCell(menu.risk.row + i - 1, 2, tostring(menu.risk.volume[i]))
				window:SetCell(menu.risk.row + i - 1, 3, string.format("%.2f   %.0f%%",
					menu.risk.volume[i] * lib:GetLotSize(menu.chart.instrument) * menu.label.price.lm / menu.portfolio.money,
					menu.risk.volume[i] * 100 / max_lots
				))
			else
				menu.risk.volume[i] = 0
				menu.risk.risks[i] = menu.risk.limits[i]
				
				if i > 1 then
					window:SetCell(menu.risk.row + i - 1, 1, string.format("%.2f%%", menu.risk.limits[i]))
				else
					window:SetCell(menu.risk.row + i - 1, 1, "#2")
				end
				
				window:SetCell(menu.risk.row + i - 1, 2, "")
				window:SetCell(menu.risk.row + i - 1, 3, "")
			end
		end
		
		menu.risk.changed = true
		menu.risk:Select()
	end
end)


function menu.risk:Select(index)
	if index then
		self.limit_index = nil
	elseif self.limit_index then
		if self.limit_index == 1 then
			index = 1
		else
			for i, v in ipairs(menu.risk.risks) do
				if v <= menu.risk.limits[self.limit_index] then
					index = i
				end
			end
		end
	end
	
	if index and index > 0 and index <= self.rows then
		if self.index ~= index then
			if self.index then
				window:SetColor(self.row + self.index - 1, 1, QTABLE_DEFAULT_COLOR)
			end
			self.index = index
			
			if not self.limit_index then
				if menu.risk.risks then
					self.limit_index = #menu.risk.limits
					for i = #menu.risk.limits, 1, -1 do
						if menu.risk.limits[i] >= menu.risk.risks[index] then
							self.limit_index = i
						end
					end
				else
					self.limit_index = index
				end
			end
			
			if menu.chart.instrument then
				self.changed = true
			end
			
			window:SetColor(self.row + index - 1, 1, RGB(192, 192, 255))
		end
	end
end


function menu.risk:Get()
	if menu.label.price.lm and menu.label.price.sl
		and lib:CheckStop(menu.chart.instrument, menu.label.price.lm > menu.label.price.sl, menu.label.price.sl)
	then
		return self.volume[self.index], menu.chart.instrument, self.volume[#self.volume]
	else
		return 0, menu.chart.instrument, 0
	end
end


end	-- of file
